///////////////////////////////////////////////////
// Election Administration Polarization
// Turnout RD Table
///////////////////////////////////////////////////

gl path "~/Library/CloudStorage/GoogleDrive-danmckinleythompson@gmail.com/My Drive/ElecAdminPolarization/How_Partisan_Is_Local_Election_Admin_Replication"


* Bring in the turnout and registration analysis data
use "$path/analysis_data/rd_reg_turnout_analysis_data.dta", clear

rdbwselect r_votes_highest_pc rv, vce(cluster election_id) kernel(uniform)
gl bw = e(h_mserd)

* Spec 1, Residualized Outcome
reg r_votes_highest_pc treat rv treat_rv if abs(rv)<$bw, vce(clust election_id)
rdtab r_votes_highest_pc treat rv election_id, est_command("reg") new("B") tau(0.02) alpha(0.05) onesided

* Spec 4, Residualized Outcome
rdrobust r_votes_highest_pc rv, vce(cluster election_id) all
rdtab r_votes_highest_pc treat rv election_id, est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided


// R-majority counties
 
rdbwselect r_votes_highest_pc rv if lag_vs_dem<0.5, vce(cluster election_id) kernel(uniform)
gl bw = e(h_mserd)

* Spec 1, Residualized Outcome
reg r_votes_highest_pc treat rv treat_rv if abs(rv)<$bw & lag_vs_dem<0.5, vce(clust election_id)
rdtab r_votes_highest_pc treat rv election_id if lag_vs_dem<0.5, est_command("reg") old("B") tau(0.02) alpha(0.05) onesided

* Spec 4, Residualized Outcome
rdrobust r_votes_highest_pc rv if lag_vs_dem<0.5, vce(cluster election_id) all
rdtab r_votes_highest_pc treat rv election_id if lag_vs_dem<0.5, est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided


// D-majority counties
 
rdbwselect r_votes_highest_pc rv if lag_vs_dem>0.5 & lag_vs_dem!=., vce(cluster election_id) kernel(uniform)
gl bw = e(h_mserd)

* Spec 1, Residualized Outcome
reg r_votes_highest_pc treat rv treat_rv if abs(rv)<$bw & lag_vs_dem>0.5 & lag_vs_dem!=., vce(clust election_id)
rdtab r_votes_highest_pc treat rv election_id if lag_vs_dem>0.5 & lag_vs_dem!=., est_command("reg") old("B") tau(0.02) alpha(0.05) onesided

* Spec 4, Residualized Outcome
rdrobust r_votes_highest_pc rv if lag_vs_dem>0.5 & lag_vs_dem!=., vce(cluster election_id) all
rdtab r_votes_highest_pc treat rv election_id if lag_vs_dem>0.5 & lag_vs_dem!=., est_command("rdrobust") old("B") tau(0.02) alpha(0.05) onesided


////
// Turnout table for D vs R counties
////


qui {
cap log close
set linesize 255
log using "$path/output/rd_table_turnout_county_types.tex", text replace
noi di "\begin{table}[t]"
noi di "\centering"
noi di "\caption{\textbf{Effect of Democratic Election Officials on Turnout.}"
noi di "\label{tab:rd_table_turnout_types}}"
noi di "\begin{tabular}{lcc|cc|cc}"
noi di "\toprule \toprule"
noi di " & \multicolumn{6}{c}{Votes per Voting-Age Resident}\\"
noi di " & \multicolumn{2}{c}{All Counties} & \multicolumn{2}{c}{Rep Counties} & \multicolumn{2}{c}{Dem Counties}\\"
noi di " & (1) & (2) & (3) & (4) & (5) & (6) \\"
noi di "\midrule"
noi di "Dem Elec Official & " %4.3f B[1,1] " & " %4.3f B[1,2] " & " %4.3f B[1,3] " & " %4.3f B[1,4] " & " %4.3f B[1,5] " & " %4.3f B[1,6] "\\"
noi di " & (" %4.3f B[2,1] ") & (" %4.3f B[2,2] ") & (" %4.3f B[2,3] ") & (" %4.3f B[2,4] ") & (" %4.3f B[2,5] ") & (" %4.3f B[2,6]  ") \\[2mm]"
noi di "\midrule"
noi di "N & " %4.0f B[3,1] " & " %4.0f B[3,2] " & " %4.0f B[3,3] " & " %4.0f B[3,4] " & " %4.0f B[3,5] " & " %4.0f B[3,6] " \\ "
noi di "Clusters & " %4.0f B[4,1] " & " %4.0f B[4,2] " & " %4.0f B[4,3] " & " %4.0f B[4,4] " & " %4.0f B[4,5] " & " %4.0f B[4,6] " \\ "
noi di "Bandwidth & " %4.2f B[5,1] " & " %4.2f B[5,2] " & " %4.2f B[5,3] " & " %4.2f B[5,4] " & " %4.2f B[5,5] " & " %4.2f B[5,6] " \\ "
noi di "BW Selection & CCT & CCT & CCT & CCT & CCT & CCT \\"
noi di "Kernel & Unif & Tri & Unif & Tri & Unif & Tri \\"
noi di "Min Detectable Effect  & " %4.3f B[7,1] " & " %4.3f B[7,2] " & " %4.3f B[7,3] " & " %4.3f B[7,4] " & " %4.3f B[7,5] " & " %4.3f B[7,6] " \\ "
noi di "\bottomrule \bottomrule"
noi di "\multicolumn{7}{p{.85\textwidth}}{\footnotesize Robust standard errors clustered by clerk election in parentheses. "
noi di "Rep counties are those where the last Republican presidential candidate received more votes than the last "
noi di "Democratic presidential candidate. Dem counties are all remaining counties. "
noi di "The outcome is first regressed on a state- and year-specific lag using all counties including those for which clerk election "
noi di "results are not available. The regression discontinuity is estimated using the residuals from that regression. "
noi di "The bandwidth row reports the number of maximum clerk win margin allowed for inclusion in each specificaiton. "
noi di "CCT refers to \cite{calonico2014robust} bandwidth selection procedure."
noi di "Min detectable effect refers to the minimum effect that a one-sided test with a 0.05 alpha would have 80\% power to detect. "
noi di "Unif refers to a uniform kernel. Tri refers to a triangular kernel.}"
noi di "\end{tabular}"
noi di "\end{table}"
log off
}
